home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15769 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  123 lines

  1. Path: newsfeeder.netgate.net!usenet
  2. From: Tamara Johnson <malihini@netgate.net>
  3. Newsgroups: comp.lang.c
  4. Subject: (no subject)
  5. Date: 21 Apr 1996 19:42:33 GMT
  6. Organization: NetGate Communications
  7. Message-ID: <4le339$95a@ss.netgate.net>
  8. NNTP-Posting-Host: d81.netgate.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  13.  
  14. /* Problem: Possible to pass a whole struct to 
  15.             a function?
  16.  
  17.             Requirement: create a function that will
  18.             printf a union regardless of type (using a flag)
  19.             using one struct containing the union and flag 
  20.             (and etc.).
  21.  
  22.   Don't need the solution, just need help passing the
  23.   the struct and how to printf (point to?) individual 
  24.   elements within. I *think* I can get the rest of it */
  25.  
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. void Function1( void *P1, void *P2);
  32. void Function2( void *P1);
  33.  
  34.  
  35.    /* main ----------------------------- */
  36.    void main()
  37.    {
  38.         union tag_1{
  39.                float f;
  40.                int i;
  41.                char string[5];
  42.                double d;
  43.                     };
  44.  
  45.         struct tag_2{
  46.                union tag_1 Union; 
  47.                int flag;
  48.                    }Flag, *fP;
  49.  
  50.        /* stuff to figure out if it's working to this point */
  51.         Flag.flag=3;
  52.         Flag.Union.f=5.3456;    
  53.          /* conversion from 'const double '
  54.             to 'float ', possible loss of data    
  55.             NOTE: works ok                   */
  56.         Flag.Union.d=78.9631;
  57.         Flag.Union.i=31;
  58.         strcpy(Flag.Union.string,"red");    
  59.  
  60.         printf("main() Flag.Union.string: %s\n", 
  61.                                            Flag.Union.string);
  62.         printf("main() Flag.Union.i     : %i\n", Flag.Union.i);
  63.         printf("main() Flag.Union.f     : %f\n", Flag.Union.f);
  64.         printf("main() Flag.Union.d     : %f\n", Flag.Union.d);
  65.  
  66.         printf("main() Flag.flag        : %i\n", Flag.flag);
  67.  
  68.       /* end of stuff - is working when Function2 is deleted.
  69.          Of course, whatever union member is initialized last
  70.          is the one that follows thru, others will be
  71.          essentially garbage */
  72.  
  73.         Function1(Flag.Union.string, Flag.flag); /* Function1' 
  74.                 :pointer mismatch for actual parameter 2 
  75.                 NOTE: but appears to be functioning ok  
  76.                       when Function2 deleted.
  77.                       except passing individual elements, 
  78.                       need to pass the whole struct */
  79.      /*
  80.         Function2(fP);
  81.      */
  82.     }
  83.    /* Function1 ------------------------------ */
  84.    
  85.    void Function1( void *P1, void *P2)
  86.    {
  87.  
  88.         printf("Function1 P1: %s\n", ( (char *)P1));
  89.         printf("Function1 P2: %i\n", ( (int *)P2));
  90.            
  91.    }
  92.    /* Function2 
  93.    
  94.    void Function2( void *P1)  /*NOTE: not working
  95.    {
  96.     /*
  97.       printf("Function2 output:\n %s\n", 
  98.                             ( (char *)P1->tag_1));
  99.        
  100.       printf("Function2\n");
  101.       if(( (char *)P1).flag==3)    /* left of '.f' must have 
  102.                                  struct/union type
  103.                                  NOTE: not working         
  104.       printf("Function2 output:\n %s\n", 
  105.         ( (char *)P1->Union.string));
  106.                 /* left of '->Union' must point to 
  107.                     struct/union
  108.                    NOTE: not working                
  109.    }                        
  110. ------------------------------ */
  111.    /* ------------------ OUTPUT ---------------------------
  112.      main() Flag.Union.string: red
  113.      main() Flag.Union.i     : 6579570
  114.      main() Flag.Union.f     : 0.000000
  115.      main() Flag.Union.d     : 78.963074
  116.      main() Flag.flag        : 3
  117.      Function1 P1: red
  118.      Function1 P2: 3
  119.                     
  120.     ----------------------------------------------------- */
  121.  
  122.  
  123.